<?php
namespace app\admin\controller;
use app\admin\controller\BaseController;
use app\common\errcode\AdminErrCode;
use app\common\errcode\AdminErrMsg;
use think\Loader;

class CatController extends BaseController
{
    protected $catModel;

    public function __construct()
    {
        parent::__construct();
        $this->catModel = new \app\admin\model\CatModel();
    }

    /**
     * 新建分类
     * @param string $cat
     */
    public function create()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Cat');
                $check = $validate->scene('create')->check($data);
                if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->catModel->addCat(trim($data['cat'])) === true) return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);
                return $this->jsonJump(AdminErrCode::ADD_CAT_FAIL,AdminErrMsg::ADD_CAT_FAIL);         
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }    
    }

    /**
     * 删除分类
     * @param int $catid
     */
    public function delete()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Cat');
                $check = $validate->scene('delete')->check($data);
                if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->catModel->deleteCat((int)$data['catid']) === true) return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);
                return $this->jsonJump(AdminErrCode::DELETE_CAT_FAIL,AdminErrMsg::DELETE_CAT_FAIL);    
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }    
    }

    /**
     * 编辑分类
     * @param int       $catid
     * @param string    $cat
     */
    public function edit()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Cat');
                $check = $validate->scene('edit')->check($data);
                if(!$check) return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->catModel->editCat((int)$data['catid'],trim($data['cat'])) === true) return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);
                return $this->jsonJump(AdminErrCode::EDIT_CAT_FAIL,AdminErrMsg::EDIT_CAT_FAIL);    
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }    
    }

    /**
     * 分类列表
     */
    public function index()
    {
        try {
            if($this->request->isPost()){
                return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS,$this->catModel->list());        
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }
}